package com.jf.controller;

import com.alibaba.fastjson.JSON;
import com.jf.bean.Banner;
import com.jf.bean.Project2;
import com.jf.service.Project2Service;
import com.jf.service.impl.Project2ServiceImpl;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@WebServlet("/project2")
public class Project2Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Project2Service project2Service = new Project2ServiceImpl();
        BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        String result = MyUtils.getRequestJson(in);
        Project2 project2 = JSON.parseObject(result,Project2.class);

        String param = request.getParameter("param");

        if(MyUtils.isStringEmpty(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/project2/project2-list.jsp").forward(request,response);
        }
        if("edit".equals(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/project2/project2-edit.jsp").forward(request,response);
        }
        if("list".equals(param)){
            int page = MyUtils.string2Int(request.getParameter("page"),1);
            int limit = MyUtils.string2Int(request.getParameter("limit"),5);
            PageInfo pageInfo = new PageInfo(page,limit);
            Project2 searchInfo = new Project2();
            searchInfo.setName(request.getParameter("name"));
            response.getWriter().println(JSON.toJSON(project2Service.getPage(pageInfo,searchInfo)));
        }
        if("info".equals(param)){
            response.getWriter().println(JSON.toJSON(project2Service.info(request.getParameter("id"))));
        }
        if("update".equals(param)){
            response.getWriter().println(JSON.toJSON(project2Service.saveOrUpdate(project2)));
        }
        if("delete".equals(param)){
            String[] ids = MyUtils.string2Array(request.getParameter("ids"));
            if(ids==null){
                ids = new String[]{project2.getId()};
            }
            response.getWriter().println(JSON.toJSON(project2Service.delete(ids)));
        }
    }
}
